﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using ARS.Control.Custom.Entity.Attributes;
using ARS.Control.Custom.Entity.Fields;

namespace ARS.Control.Custom.Entity.Common
{
    public class SettingToForm
    {
        /// <summary>
        /// 从DataRow获取form
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public Forms.Form GetForm(DataRow row)
        {
            string code = row["Code"].ToString();
            switch (code)
            {
                case "KeyField":
                    break;
                case "CharField":
                    ARS.Control.Custom.Entity.Forms.CharForm charForm =
                        new ARS.Control.Custom.Entity.Forms.CharForm();
                    SetFieldConfig(charForm.Field, row);
                    return charForm;
                    break;
                case "ForeignKeyField":
                    string otherId = row["OtherId"].ToString();
                    DataSet ds =
                        ARS.DataBase.Provider.DbFactory.DbProvider.Query(
                            "select * from Ars_Control_DbSelectHtmlSelect_AttrAll where ARS_Control_DbSelectHtmlSelect_Attr_Id=" +
                            otherId);
                    DataTable dt_select = ds.Tables[0];
                    DataRow row_select = dt_select.Rows[0];
                    ARS.Control.Custom.Entity.Forms.SelectForm form =
                        new ARS.Control.Custom.Entity.Forms.SelectForm();
                    var field = new ARS.Control.Custom.Entity.Fields.ForeignKeyField();
                    SetDbSelectConfig(field, row_select);
                    SetFieldConfig(field, row);
                    form.Field = field;
                    return form;
                    //this.Panel1.Controls.Add(form);
                    break;
                case "ChoicesDialogField":
                    string coid = row["OtherId"].ToString();
                    DataSet ds_cdf =
                        ARS.DataBase.Provider.DbFactory.DbProvider.Query(
                            "select * from ARS_Control_ChoicesDialogField_AllConfig where ARS_Control_ChoicesDialogField_Attr_Id=" +
                            coid);
                    DataTable dt_cdf = ds_cdf.Tables[0];
                    DataRow row_cdf = dt_cdf.Rows[0];
                    ARS.Control.Custom.Entity.Forms.ChoicesDialogForm choicesDialogForm =
                        new ARS.Control.Custom.Entity.Forms.ChoicesDialogForm();

                    ChoicesDialogField field_cdf = new ChoicesDialogField();
                    SetDbSelectConfig(field_cdf, row_cdf);
                    SetFieldConfig(field_cdf, row);

                    field_cdf.ListUrl = row_cdf["ListUrl"].ToString();
                    field_cdf.DataValueClassName = row_cdf["DataValueClassName"].ToString();
                    field_cdf.DataTextClassName = row_cdf["DataTextClassName"].ToString();
                    field_cdf.MaxChoicesNum = Convert.ToInt32(row_cdf["MaxChoicesNum"]);
                    field_cdf.IsEncryptKey = Convert.ToBoolean(row_cdf["IsEncryptKey"]);
                    choicesDialogForm.Field = field_cdf;


                    return choicesDialogForm;
                    //this.Panel1.Controls.Add(choicesDialogForm);
                    break;
                case "UserField":
                    //string uid = row["OtherId"].ToString();
                    //DataSet ds_user =
                    //    ARS.DataBase.Provider.DbFactory.DbProvider.Query(
                    //        "select * from ARS_Control_ChoicesDialogField_AllConfig where ARS_Control_ChoicesDialogField_Attr_Id=" +
                    //        uid);
                    //DataTable dt_user = ds_user.Tables[0];
                    //DataRow row_user = dt_user.Rows[0];

                    break;
                case "DateTimeAutoField":
                    break;
            }
            return null;
        }

        private void SetDbSelectConfig(ForeignKeyField field, DataRow row)
        {
            field.SelectDbSelectConfig = new ForeignKeyConfig();
            field.SelectDbSelectConfig.DataTextField = row["DataTextField"].ToString();
            field.SelectDbSelectConfig.DataValueField = row["DataValueField"].ToString();
            field.SelectDbSelectConfig.FirstOptionValue = row["FirstValue"].ToString();
            field.SelectDbSelectConfig.FirstOptionText = row["FirstText"].ToString();
            field.SelectDbSelectConfig.OrderField = row["OrderField"].ToString();
            field.SelectDbSelectConfig.StrWhere = row["StrWhere"].ToString();
            field.SelectDbSelectConfig.SelectFields = row["SelectFields"].ToString();
            field.SelectDbSelectConfig.TableName = row["TableName"].ToString();
            field.SelectDbSelectConfig.KeyField = row["KeyField"].ToString();
        }

        private void SetFieldConfig(Field field, DataRow row)
        {
             field.Verbose_Name = row["Verbose_Name"].ToString();
             field.Html_Control_Name = row["Html_Control_Name"].ToString();
             field.Verbose_Name = row["Verbose_Name"].ToString();//Help_Text
             field.Help_Text = row["Help_Text"].ToString();
             //field_cdf.Html_Control_Name = row["Html_Control_Name"].ToString();
             field.DefaultValue = row["DefaultValue"].ToString();
             field.UniqueWhere = row["UniqueWhere"].ToString();
             field.IsUnique = Convert.ToBoolean(row["IsUnique"]);
             field.IsAllowAdd = Convert.ToBoolean(row["IsAllowAdd"]);
             field.IsAllowUpdate = Convert.ToBoolean(row["IsAllowUpdate"]);
             field.IsAllowBlank = Convert.ToBoolean(row["IsAllowUpdate"]);
             field.Visible = Convert.ToBoolean(row["IsAllowUpdate"]);
             field.IsShow = Convert.ToBoolean(row["IsAllowUpdate"]);
        }

        /// <summary>
        /// 从DataTable获取form列表
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public List<Forms.Form> GetFormList(DataTable dt)
        {
            List<Forms.Form> list = new List<Forms.Form>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(this.GetForm(row));
            }
            return list;
        }
        /// <summary>
        /// 输出控件列表到form
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="control"></param>
        public void ResponseForm(DataTable dt,WebControl control)
        {
            foreach (DataRow row in dt.Rows)
            {
                control.Controls.Add(this.GetForm(row));
            }
        }

        public DataTable GetDataTable()
        {
            return null;
        }

    }
}
